package cn.edu.scau.cmi.hombio.service;

import cn.edu.scau.cmi.hombio.dao.EmployeeDAO;
import cn.edu.scau.cmi.hombio.dao.ExceptDAO;
import cn.edu.scau.cmi.hombio.dao.ExceptionTypeDAO;
import cn.edu.scau.cmi.hombio.dao.TaskDAO;

import cn.edu.scau.cmi.hombio.domain.Employee;
import cn.edu.scau.cmi.hombio.domain.Except;
import cn.edu.scau.cmi.hombio.domain.ExceptionType;
import cn.edu.scau.cmi.hombio.domain.Task;

import java.util.List;
import java.util.Set;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Service;

import org.springframework.transaction.annotation.Transactional;

/**
 * Spring service that handles CRUD requests for Except entities
 * 
 */

@Service("ExceptService")

@Transactional
public class ExceptServiceImpl implements ExceptService {

	/**
	 * DAO injected by Spring that manages Employee entities
	 * 
	 */
	@Autowired
	private EmployeeDAO employeeDAO;

	/**
	 * DAO injected by Spring that manages Except entities
	 * 
	 */
	@Autowired
	private ExceptDAO exceptDAO;

	/**
	 * DAO injected by Spring that manages ExceptionType entities
	 * 
	 */
	@Autowired
	private ExceptionTypeDAO exceptionTypeDAO;

	/**
	 * DAO injected by Spring that manages Task entities
	 * 
	 */
	@Autowired
	private TaskDAO taskDAO;

	/**
	 * Instantiates a new ExceptServiceImpl.
	 *
	 */
	public ExceptServiceImpl() {
	}

	/**
	 * Delete an existing ExceptionType entity
	 * 
	 */
	@Transactional
	public Except deleteExceptExceptionType(Integer except_id, Integer related_exceptiontype_id) {
		Except except = exceptDAO.findExceptByPrimaryKey(except_id, -1, -1);
		ExceptionType related_exceptiontype = exceptionTypeDAO.findExceptionTypeByPrimaryKey(related_exceptiontype_id, -1, -1);

		except.setExceptionType(null);
		related_exceptiontype.getExcepts().remove(except);
		except = exceptDAO.store(except);
		exceptDAO.flush();

		related_exceptiontype = exceptionTypeDAO.store(related_exceptiontype);
		exceptionTypeDAO.flush();

		exceptionTypeDAO.remove(related_exceptiontype);
		exceptionTypeDAO.flush();

		return except;
	}

	/**
	 * Save an existing Task entity
	 * 
	 */
	@Transactional
	public Except saveExceptTask(Integer id, Task related_task) {
		Except except = exceptDAO.findExceptByPrimaryKey(id, -1, -1);
		Task existingtask = taskDAO.findTaskByPrimaryKey(related_task.getId());

		// copy into the existing record to preserve existing relationships
		if (existingtask != null) {
			existingtask.setId(related_task.getId());
			existingtask.setTaskNumber(related_task.getTaskNumber());
			existingtask.setCreateTime(related_task.getCreateTime());
			existingtask.setStartTime(related_task.getStartTime());
			existingtask.setEndTime(related_task.getEndTime());
			existingtask.setMemo(related_task.getMemo());
			related_task = existingtask;
		} else {
			related_task = taskDAO.store(related_task);
			taskDAO.flush();
		}

		except.setTask(related_task);
		related_task.getExcepts().add(except);
		except = exceptDAO.store(except);
		exceptDAO.flush();

		related_task = taskDAO.store(related_task);
		taskDAO.flush();

		return except;
	}

	/**
	 * Delete an existing Task entity
	 * 
	 */
	@Transactional
	public Except deleteExceptTask(Integer except_id, Integer related_task_id) {
		Except except = exceptDAO.findExceptByPrimaryKey(except_id, -1, -1);
		Task related_task = taskDAO.findTaskByPrimaryKey(related_task_id, -1, -1);

		except.setTask(null);
		related_task.getExcepts().remove(except);
		except = exceptDAO.store(except);
		exceptDAO.flush();

		related_task = taskDAO.store(related_task);
		taskDAO.flush();

		taskDAO.remove(related_task);
		taskDAO.flush();

		return except;
	}

	/**
	 * Save an existing Except entity
	 * 
	 */
	@Transactional
	public void saveExcept(Except except) {
		Except existingExcept = exceptDAO.findExceptByPrimaryKey(except.getId());

		if (existingExcept != null) {
			if (existingExcept != except) {
				existingExcept.setId(except.getId());
				existingExcept.setBarcode(except.getBarcode());
				existingExcept.setReportTime(except.getReportTime());
				existingExcept.setMemo(except.getMemo());
			}
			except = exceptDAO.store(existingExcept);
		} else {
			except = exceptDAO.store(except);
		}
		exceptDAO.flush();
	}

	/**
	 * Load an existing Except entity
	 * 
	 */
	@Transactional
	public Set<Except> loadExcepts() {
		return exceptDAO.findAllExcepts();
	}

	/**
	 * Save an existing Employee entity
	 * 
	 */
	@Transactional
	public Except saveExceptEmployee(Integer id, Employee related_employee) {
		Except except = exceptDAO.findExceptByPrimaryKey(id, -1, -1);
		Employee existingemployee = employeeDAO.findEmployeeByPrimaryKey(related_employee.getId());

		// copy into the existing record to preserve existing relationships
		if (existingemployee != null) {
			existingemployee.setId(related_employee.getId());
			existingemployee.setNumber(related_employee.getNumber());
			existingemployee.setName(related_employee.getName());
			existingemployee.setPassword(related_employee.getPassword());
			existingemployee.setLogOn(related_employee.getLogOn());
			existingemployee.setRfid(related_employee.getRfid());
			existingemployee.setSex(related_employee.getSex());
			existingemployee.setBirthday(related_employee.getBirthday());
			existingemployee.setState(related_employee.getState());
			existingemployee.setMemo(related_employee.getMemo());
			related_employee = existingemployee;
		}

		except.setEmployee(related_employee);
		related_employee.getExcepts().add(except);
		except = exceptDAO.store(except);
		exceptDAO.flush();

		related_employee = employeeDAO.store(related_employee);
		employeeDAO.flush();

		return except;
	}

	/**
	 * Delete an existing Except entity
	 * 
	 */
	@Transactional
	public void deleteExcept(Except except) {
		exceptDAO.remove(except);
		exceptDAO.flush();
	}

	/**
	 * Return a count of all Except entity
	 * 
	 */
	@Transactional
	public Integer countExcepts() {
		return ((Long) exceptDAO.createQuerySingleResult("select count(o) from Except o").getSingleResult()).intValue();
	}

	/**
	 */
	@Transactional
	public Except findExceptByPrimaryKey(Integer id) {
		return exceptDAO.findExceptByPrimaryKey(id);
	}

	/**
	 * Save an existing ExceptionType entity
	 * 
	 */
	@Transactional
	public Except saveExceptExceptionType(Integer id, ExceptionType related_exceptiontype) {
		Except except = exceptDAO.findExceptByPrimaryKey(id, -1, -1);
		ExceptionType existingexceptionType = exceptionTypeDAO.findExceptionTypeByPrimaryKey(related_exceptiontype.getId());

		// copy into the existing record to preserve existing relationships
		if (existingexceptionType != null) {
			existingexceptionType.setId(related_exceptiontype.getId());
			existingexceptionType.setName(related_exceptiontype.getName());
			existingexceptionType.setMemo(related_exceptiontype.getMemo());
			related_exceptiontype = existingexceptionType;
		}

		except.setExceptionType(related_exceptiontype);
		related_exceptiontype.getExcepts().add(except);
		except = exceptDAO.store(except);
		exceptDAO.flush();

		related_exceptiontype = exceptionTypeDAO.store(related_exceptiontype);
		exceptionTypeDAO.flush();

		return except;
	}

	/**
	 * Return all Except entity
	 * 
	 */
	@Transactional
	public List<Except> findAllExcepts(Integer startResult, Integer maxRows) {
		return new java.util.ArrayList<Except>(exceptDAO.findAllExcepts(startResult, maxRows));
	}

	/**
	 * Delete an existing Employee entity
	 * 
	 */
	@Transactional
	public Except deleteExceptEmployee(Integer except_id, Integer related_employee_id) {
		Except except = exceptDAO.findExceptByPrimaryKey(except_id, -1, -1);
		Employee related_employee = employeeDAO.findEmployeeByPrimaryKey(related_employee_id, -1, -1);

		except.setEmployee(null);
		related_employee.getExcepts().remove(except);
		except = exceptDAO.store(except);
		exceptDAO.flush();

		related_employee = employeeDAO.store(related_employee);
		employeeDAO.flush();

		employeeDAO.remove(related_employee);
		employeeDAO.flush();

		return except;
	}
}
